package main

import "fmt"

type Animal interface {
	GetName() string
}

// 使用者
func Name(animal Animal) string {
	return animal.GetName()
}
func Inspect(animal Animal) {
	fmt.Println("Type Assertion")
	fmt.Printf("%T,%v\n", animal, animal)
	/*
		type assertion类型断言
		需要接收返回值进行判断，否则可能会报panic
	*/
	if duck, ok := animal.(Duck); ok {
		fmt.Println("duck:", duck)
	} else {
		fmt.Println("this is not a duck")
	}

	fmt.Println("Type Switch")
	switch animal.(type) {
	case Duck:
		fmt.Println("this is a Duck")
	case Dog:
		fmt.Println("this is a Dog")
	}
}

// 实现者
type Duck struct {
	name string
}

func (duck Duck) GetName() string {
	return duck.name
}

type Dog struct {
	name string
}

func (dog Dog) GetName() string {
	return dog.name
}
func main() {
	var animal Animal
	animal = Duck{"鸭子"}
	fmt.Println(Name(animal))
	Inspect(animal)
}
